﻿namespace SuperMarieApi.Log.LogAgent.ILog
{
    using System;
    using System.IO;
    using System.Reflection;

    using SuperMarieApi.Log.LogAgent.LogAgent;

    /// <summary>
    /// Log4Net日志
    /// </summary>
    internal class Log4net : BaseLog
    {
        #region 初始化Log
        /// <summary>
        /// 默认日志配置
        /// </summary>
        protected override void DefaultLogConfig()
        {
            this.logInfo.LogName = "log4net";
            this.logInfo.LogFileName = "log4net\\log4net.dll";
            this.logInfo.LogAssembly = null;
            this.logInfo.LogConfigName = "log4net\\log4net.config";
            this.logInfo.LogConfigFile = new FileInfo(this.logInfo.LogConfigName);
            this.logInfo.AdapterConfigFile = null;
            this.FrameIndex = 0;
            this.IsLogStackTrace = false;
        }
        /// <summary>
        /// 初始化日志程序集函数
        /// </summary>
        protected override void IniLogAssembly()
        {
            var ass = base.logInfo.LogAssembly;
            var methodConfigureAndWatch = this.getMethodConfigureAndWatch(ass);
            methodConfigureAndWatch.Invoke(null, new object[] { this.logInfo.LogConfigFile });
            var methodGetLogger = this.getMethodGetLogger(ass);
            this.log = methodGetLogger.Invoke(null, new object[] { this.GetType() });
        }
        #endregion

        #region 初始化日志类
        private MethodInfo getMethod(Assembly ass, string className, string methodName, Type[] types)
        {
            Type t = ass.GetType(className);
            var result = t.GetMethod(methodName, types);
            return result;
        }
        private MethodInfo getMethodConfigureAndWatch(Assembly ass)
        {
            var className = "log4net.Config.XmlConfigurator";
            var methodName = "ConfigureAndWatch";
            return this.getMethod(ass, className, methodName, new Type[] { typeof(FileInfo) });
        }
        private MethodInfo getMethodGetLogger(Assembly ass)
        {
            var className = "log4net.LogManager";
            var methodName = "GetLogger";
            return this.getMethod(ass, className, methodName, new Type[] { typeof(Type) });
        }
        #endregion
    }
}
